﻿Public Class frmCIDTools
    Dim Strsql As String
    Dim mysql As New ClsSQLhelper
#Region "Function"
    Sub Load_PPData()
        Try
            Dim DT_Person As New DataTable
            Dim DT_Patient As New DataTable
            With GridPersons
                If TxtPID.Text <> "" Then
                    Strsql = "Select person_id,patient_hn,cid from person where person_id = '" & Me.TxtPID.Text & "'"
                    DT_Person = mysql.GetMYSQLDataTable(Strsql, "patient", True)
                    .RowHeadersVisible = False
                    .AllowDrop = False
                    .AllowUserToAddRows = False
                    .AllowUserToDeleteRows = False
                    .AllowUserToOrderColumns = True
                    .AllowUserToResizeColumns = True
                    .AllowUserToResizeRows = False
                    .MultiSelect = False
                    .AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.DisplayedCells
                    .AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells
                    .DataSource = DT_Person
                End If
            End With
            With GridPatients
                Strsql = "Select hn,cid from patient where hn = '" & Me.TxtHN.Text & "'"
                DT_Patient = mysql.GetMYSQLDataTable(Strsql, "person", True)
                .RowHeadersVisible = False
                .AllowDrop = False
                .AllowUserToAddRows = False
                .AllowUserToDeleteRows = False
                .AllowUserToOrderColumns = True
                .AllowUserToResizeColumns = True
                .AllowUserToResizeRows = False
                .MultiSelect = False
                .AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.DisplayedCells
                .AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells
                .DataSource = DT_Patient
            End With

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub
#End Region

    Private Sub BtnForceUpdateZeroCID_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnForceUpdateZeroCID.Click
        Try

            MsgBox("การ Update CID แบบไม่ใช้ HOSxP อาจมีความเสี่ยง ผู้ดูแลระบบควรตรวจสอบก่อน update ทุกครั้ง", MsgBoxStyle.Exclamation, "คำเตือน")
            If MsgBox("กรุณาตรวจสอบให้แน่ใจก่อนทำการ Update ว่าไม่มี CID จริงๆ", MsgBoxStyle.Exclamation + MsgBoxStyle.YesNo, "คำเตือน ") = MsgBoxResult.No Then Exit Sub

            If Me.GridPatients.RowCount <> 0 Then
                Strsql = "Update Patient set cid = '"
                If Me.OptZeroUpdate.Checked = True Then
                    Strsql = Strsql & "0000000000000"
                Else
                    Strsql = Strsql & "000000" & Me.TxtHN.Text
                End If
                Strsql = Strsql & "'  where hn ='" & Me.TxtHN.Text & "'"
                mysql.MySQLExecute(Strsql, True)
            End If
            If Me.GridPersons.RowCount <> 0 And Me.TxtPID.Text <> "" Then
                Strsql = "Update Person set cid = '"
                If Me.OptZeroUpdate.Checked = True Then
                    Strsql = Strsql & "0000000000000"
                Else
                    Strsql = Strsql & "000000" & Me.TxtHN.Text
                End If
                Strsql = Strsql & "'  where person_id ='" & Me.TxtPID.Text & "'"
                mysql.MySQLExecute(Strsql, True)
            End If
            MsgBox("Update Complete", MsgBoxStyle.Information, "ผลการ update")
            Load_PPData()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub TxtCID_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TxtCID.TextChanged

    End Sub

    Private Sub frmCIDTools_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Load_PPData()
        Me.lblPatientSQL.Text = "Update Patient set cid = '0000000000000' where hn ='" & Me.TxtHN.Text & "'"
        If Me.TxtPID.Text <> "" Then lblPersonSQL.Text = "Update Person set cid = '0000000000000' where person_id ='" & Me.TxtPID.Text & "'"
        If Me.TxtPID.Text <> "" Then Me.GP_Person.GradientHeaderText = "Person PID = " & Me.TxtPID.Text

        Me.GPPatient.GradientHeaderText = "Patient HN = " & Me.TxtHN.Text

    End Sub

    Private Sub OptZeroUpdate_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OptZeroUpdate.CheckedChanged
        Me.lblPatientSQL.Text = "Update Patient set cid = '0000000000000' where hn ='" & Me.TxtHN.Text & "'"
        If Me.TxtPID.Text <> "" Then lblPersonSQL.Text = "Update Person set cid = '0000000000000' where person_id ='" & Me.TxtPID.Text & "'"
    End Sub

    Private Sub OptHN_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OptHN.CheckedChanged
        Me.lblPatientSQL.Text = "Update Patient set cid = '000000" & Me.TxtHN.Text & "' where hn ='" & Me.TxtHN.Text & "'"
        If Me.TxtPID.Text <> "" Then lblPersonSQL.Text = "Update Person set cid = '000000" & Me.TxtHN.Text & "' where person_id ='" & Me.TxtPID.Text & "'"
    End Sub
End Class